function Get-PortPid { param([int]$port); $result = netstat -ano | Select-String ":$port"; if ($result) { $portPid = $result.ToString().Split()[-1]; return $portPid } else { return $null } }
function Show-PortInfo { param([int]$port); $result = netstat -ano | Select-String ":$port"; if ($result) { return $result } else { return "No process found using port $port" } }
function Kill-PortProcess { param([int]$port); $portPid = Get-PortPid $port; if ($portPid) { $proc = Get-Process -Id $portPid -ErrorAction SilentlyContinue; if ($proc) { Write-Host "Process information:"; $proc | Format-Table Id, ProcessName, Path -AutoSize; Stop-Process -Id $portPid -Force; Write-Host "Process terminated" } else { Write-Host "Cannot find process with ID $portPid" } } else { Write-Host "No process found using port $port" } }
